<template>
	<view class="content">
		<view class="certi_top">
			<view class="title">请确保本人操作</view>
			<view style="font-size: 26rpx;color: #999;font-weight: 400;margin-bottom: 57rpx;">
				请保持正脸在取景框内完成识别
			</view>
			<view style="height: 400rpx;width: 400rpx;">
				<image class="center_img" v-show="imgShow" src="../../static/confir_face.gif" mode="aspectFit"></image>
			</view>
		</view>
		<view class="certi_tip">
			<view class="certi_tip_cell">
				<image src="../../static/phone.png" mode=""></image>
				<view class="cell_name">正对手机</view>
			</view>
			<view class="certi_tip_cell">
				<image src="../../static/light.png" mode=""></image>
				<view class="cell_name">光线充足</view>
			</view>
			<view class="certi_tip_cell">
				<image src="../../static/smail.png" mode=""></image>
				<view class="cell_name">脸无遮挡</view>
			</view>
		</view>
		<view class="open">
			<view class="open_btn" @click="openConfirm()">开启人脸识别</view>
			<w-compress ref='wCompress' />
		</view>
	</view>
</template>

<script>
	import WCompress from '@/components/w-compress/w-compress.vue'
	export default {
		data() {
			return {
				imgShow: true,
				tempFilesName: "",
				baseUrl: "https://www.signzg.com",
				contractImg: "",
				tempFilePaths:'',
				urlInfo: null
			}
		},
		components: {
			WCompress
		},
		onLoad(){
			var url = window.location.search;
			var theRequest = new Object();
			if (url.indexOf('?') != -1) {
				var str = url.substr(1);
				var strs = str.split('&');
				for (var i = 0; i < strs.length; i++) {
					theRequest[strs[i].split('=')[0]] = strs[i].split('=')[1];
				}
			}
			theRequest.enterpriseId = decodeURIComponent(theRequest.enterpriseId)
			theRequest.idcard = decodeURIComponent(theRequest.idcard)
			theRequest.legalPerson = decodeURIComponent(theRequest.legalPerson)
			// theRequest.userToken = decodeURIComponent(theRequest.userToken)
			theRequest.type = decodeURIComponent(theRequest.type)
			this.urlInfo = theRequest
			console.log('路由参数:', this.urlInfo)
			window.top.postMessage("open", "*")
		},
		methods: {
			openConfirm() {
				// this.imgShow = false;
				uni.chooseImage({
					count: 1, //默认9
					sizeType: ['compressed'], //可以指定是原图'original',还是压缩图'compressed'，默认二者都有
					sourceType: ['camera'], //调用相机,做不了内嵌相机
					success: (res) => {
						this.tempFilePaths = res.tempFiles[0].path;
						this.tempFilesName = res.tempFiles[0].name;
						//压缩图片
						if (Math.ceil(res.tempFiles[0].size / 1024) > 30) {
							uni.showLoading({
								title: '图片压缩中...',
								mask: true
							})
							this.$refs.wCompress.start(res.tempFilePaths[0], {
									pixels: 400000, // 最大分辨率，默认二百万
									quality: 0.5, // 压缩质量，默认0.8
									type: 'jpg', // 图片类型，默认jpg
									base64: true, // 是否返回base64，默认false，非H5有效
								})
								.then(res => {
									this.tempFilePaths= res;
									this.uploadFile()
									uni.hideLoading()
								})
								.catch(e => {
									uni.hideLoading()
								})
						}else{
							this.uploadFile()
						}
					},

				});
			},
			
			uploadFile(){
				//上传图片
				uni.showLoading({
					title: "上传中"
				})
				uni.uploadFile({
					url: this.baseUrl + '/upload/image',
					filePath: this.tempFilePaths,
					name: "image",
					success: (res) => {
						uni.hideLoading();
						const req = JSON.parse(res.data);
						if (req.code == 200) {
							this.contractImg = req.message;
							uni.showLoading({
								title: "认证中"
							})
							const {enterpriseId, idcard, legalPerson,phone,type} = this.urlInfo
							let data;
							// let header;
							let reqApiFn = '';
							if(type==0){// 0个人1企业
								reqApiFn='realNameAuthentication'
								data = {
									userId:enterpriseId,
									idNumber:idcard,
									userName:legalPerson,
									faceImg: this.contractImg,
									userPhone:phone,
								};
								// header={
								// 	'Access-Control-Allow-Headers':userToken
								// }
							}else if(type==1){
								reqApiFn='authenticationSuccessInfo'
								data = {
									enterpriseId,
									idcard,
									legalPerson,
									faceImg: this.contractImg
								};
							}
							console.log(reqApiFn,data);
							this.$api[reqApiFn](data)
								.then(res => {
									if (res.code == 200) {
										uni.hideLoading();
										uni.navigateTo({
											url: '/pages/facRecognition/certificationSus'
										})
									} else {
										uni.hideLoading();
										uni.navigateTo({
											url: '/pages/facRecognition/certificationFail'
										})
									}
								})
								.catch(err => {
									uni.hideLoading();
									console.log(err);
								})
						} else {
							uni.showToast({
								title: req.message,
								icon: "none"
							})
						}
					},
					fail: (err) => {
						uni.hideLoading();
						uni.showToast({
							title: err,
							icon: "none"
						})
					}
				})
			},
		}
	}
</script>

<style lang="less">
	.content {
		font-family: PingFang SC;

		.certi_top {
			text-align: center;
			color: #111;
			font-size: 34rpx;
			font-weight: bold;

			.title {
				margin: 79rpx auto 10rpx;
			}

			.bg_img {
				width: 750rpx;
				height: 400rpx;
				vertical-align: middle;
			}

			.center_img {
				position: absolute;
				top: 144rpx;
				left: 50%;
				transform: translateX(-50%);
				width: 400rpx;
				height: 400rpx;
			}

		}

		.certi_tip {
			display: flex;
			align-items: center;
			justify-content: space-between;
			padding: 0 100rpx;
			margin-top: 60rpx;

			.certi_tip_cell {
				text-align: center;

				image {
					width: 58rpx;
					height: 58rpx;
					vertical-align: middle;
				}

				.cell_name {
					color: #222;
					font-size: 26rpx;
					margin-top: 17rpx;
				}
			}
		}

		.open {
			padding: 0 100rpx;
			margin-top: 72rpx;

			.open_btn {
				text-align: center;
				height: 88rpx;
				line-height: 88rpx;
				background: #EA0014;
				border-radius: 44rpx;
				font-size: 30rpx;
				font-family: Hiragino Sans GB;
				font-weight: normal;
				color: #FFFFFF;
			}
		}
	}
</style>
